﻿using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {

	public static float enemySpeed = 2.0f;
	public static int enemyHP = 3;

	// ampumisjutskat
	public GameObject bullet;
	public static float tFiring = 1.5f;
	public static float t = 0f;


	// sekoäly
	public static float targeting_distance = 20f;
	public static float dist_to_target = 9999f;
	public GameObject target;
	public Vector3 targetpos;

	// Use this for initialization
	void Start () {
	
	}


	// Update is called once per frame
	void Update () {

		Targeting ();
		Moving ();
		Shooting ();

	}


	void Targeting()
	{
		// jos kohdetta ei vielä ole löytynyt, niin etsitään
		if (target == null)
		{
			// etsitään kohde
			target = GameObject.FindGameObjectWithTag("Player");
			
			// lasketaan etäisyys
			dist_to_target = Vector3.Distance(transform.position, target.transform.position);
			
			// jos liian kaukana, niin unohdetaan
			if(dist_to_target > targeting_distance)
			{
				target = null;

				// mennään rändömiin sijaintiin
				dist_to_target = Vector3.Distance(transform.position, targetpos);
				if(dist_to_target < 3f || dist_to_target > targeting_distance)
				{
					targetpos.x = Random.Range (-targeting_distance,targeting_distance);
					targetpos.y = Random.Range (-targeting_distance,targeting_distance);
					targetpos += transform.position;
				}
			}
		}
		else// kohde on löytynyt ja se on tarpeeks lähellä
		{
			targetpos = target.transform.position;

			// ennakointi
			targetpos.x += target.rigidbody2D.velocity.x * dist_to_target * 0.2f;
			targetpos.y += target.rigidbody2D.velocity.y * dist_to_target * 0.2f;
		}
	}
	void Moving()
	{
		LookAtZ (gameObject,targetpos);
		gameObject.transform.Rotate (new Vector3(0,0,-90f));
		
		// liikkuminen
		rigidbody2D.velocity = -transform.up * enemySpeed;
	}
	
	void Shooting()
	{
		// ei ammuta jos ei ole kohdetta
		if (target == null)
			return;

		//ampuminen
		t += Time.deltaTime; // aika kuluu
		
		
		// ampuu
		if (t >= tFiring) {
			
			t = 0f;// nollataan aikalaskuri
			
			GameObject clone;
			
			// luodaan bulletista klooni
			clone = Instantiate (bullet,
			                     transform.position,
			                     transform.rotation) as GameObject;
			
			// laitetaan vauhtia luodille
			clone.rigidbody2D.velocity = -transform.up * 20f;
			clone.transform.Translate(new Vector3(0, -0.7f, 0));
			
			//
			LookAtZ(clone, clone.transform.position + (Vector3)clone.rigidbody2D.velocity);
			
			// klooni tuhotaan 3 sekunnin päästä
			Destroy (clone, 2.0f);
		}
	}

	void LookAtZ(GameObject ob, Vector3 target_pos)
	{
		Vector3 dir = target_pos-ob.transform.position;
		dir.Normalize();
		
		Quaternion target_q = ob.transform.rotation;
		
		if(dir.x > 0f && dir.x > Mathf.Abs(dir.y))// oikeelle
			target_q = Quaternion.LookRotation(target_pos - ob.transform.position,Vector3.down);
		if(dir.x < 0f && dir.x < -Mathf.Abs(dir.y))// vasemmalle
			target_q = Quaternion.LookRotation(target_pos - ob.transform.position,Vector3.up);
		
		if(dir.y > 0f && dir.y > Mathf.Abs(dir.x))// ylös
			target_q = Quaternion.LookRotation(target_pos - ob.transform.position,Vector3.right);
		if(dir.y < 0f && dir.y < -Mathf.Abs(dir.x))// alas
			target_q = Quaternion.LookRotation(target_pos - ob.transform.position,Vector3.left);
		
		target_q.x=0;
		target_q.y=0;
		
		ob.transform.rotation = target_q;
	}

	void OnCollisionEnter2D()
	{
		enemyHP --; // pienennetään hitpointeja

		//jos hiparit loppuu, niin tuhoudutaan
		if (enemyHP <= 0) {
			if (GameManager.player_destroyed == false){
					GameManager.playerScrap++;
			}
			Destroy (gameObject,0.01f);
		}
	
	}
	void OnDrawGizmos()
	{
		Gizmos.DrawLine (transform.position,targetpos);
	}
}
